package cn.magictodo.weblog.common.domain.mapper;

import cn.magictodo.weblog.common.domain.dos.UserDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.time.LocalDateTime;

/**
 * userMapper接口
 *
 * @author liu
 * #time 2023-11-17 16:29
 */
public interface UserMapper extends BaseMapper<UserDO> {

    default UserDO findByUserName(String username){
        LambdaQueryWrapper<UserDO> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(UserDO::getUsername,username);
        return this.selectOne(objectLambdaQueryWrapper);

    };

    default int updatePasswordByUsername(String username, String password) {
        LambdaUpdateWrapper<UserDO> wrapper = new LambdaUpdateWrapper<>();
        // 设置要更新的字段
        wrapper.set(UserDO::getPassword, password);
        wrapper.set(UserDO::getUpdateTime, LocalDateTime.now());
        // 更新条件
        wrapper.eq(UserDO::getUsername, username);

        return update(null, wrapper);
    }
}